package org.eggermont.hm.cluster;

public class BlockCluster {

    private Cluster rowCluster = null;

    private Cluster colCluster = null;

    private BlockCluster[][] sons = new BlockCluster[0][0];

    private Admissibility adm = Admissibility.INADMISSIBLE;

    public BlockCluster(Cluster rowCluster, Cluster colCluster,
            Admissibility adm) {
        this(rowCluster, colCluster, 0, 0);
        this.adm = adm;
    }

    public BlockCluster(Cluster rowCluster, Cluster colCluster, int blockRows,
            int blockCols) {
        this.rowCluster = rowCluster;
        this.colCluster = colCluster;
        this.sons = new BlockCluster[blockCols][blockRows];
        this.adm = Admissibility.INADMISSIBLE;
    }

    public static BlockCluster buildBlockCluster(Cluster row, Cluster col,
            AdmissibilityCondition c) {

        if (c.isAdmissible(row, col)) {
            return new BlockCluster(row, col, Admissibility.NORMAL_ADMISSIBLE);
        }

        int blockRows = row.getNrOfSons();
        int blockCols = col.getNrOfSons();

        if (blockRows > 0 && blockCols > 0) {
            BlockCluster bc = new BlockCluster(row, col, blockRows, blockCols);
            for (int j = 0; j < blockCols; ++j) {
                for (int i = 0; i < blockRows; ++i) {
                    bc.setSon(i, j, buildBlockCluster(row.getSon(i), col
                            .getSon(j), c));
                }
            }
            return bc;
        }

        return new BlockCluster(row, col, Admissibility.INADMISSIBLE);
    }

    private void setSon(int i, int j, BlockCluster bc) {
        sons[j][i] = bc;
    }

}
